<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import serachUserid from '@/components/serachUserid/index.vue';
  import serachGift from '@/components/serachGift/index.vue';
  const formLabelWidth = '160px'
  const dataForm = ref(),
  serachGiftRef = ref();
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    serachButLoadng:false
  })
  interface formQuert {
    id:number
    color_gift_id:number
    user_id:number
    pop_gift_id:number
    delete_pop:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    color_gift_id: [{ required: true, message: "请输入彩蛋礼物ID", trigger: "blur" }],
    user_id: [{ required: true, message: "请输入用户UUID", trigger: "blur" }],
    pop_gift_id: [{ required: true, message: "请选择爆出礼物", trigger: "blur" }],
    delete_pop: [{ required: true, message: "请选择是否从礼物池中扣除", trigger: "blur" }],
  }
  const colorGiftInput = ()=>{
    if(serachGiftRef.value) serachGiftRef.value.removeQuery()
  }
  const getGiftList = async()=>{
    if(!form.color_gift_id) return ToastWarning('请先输入彩蛋礼物ID')
    action.serachButLoadng = true
    const res = await propApi.colorWhitePoolGift({
      color_gift_id:form.color_gift_id
    })
    action.serachButLoadng = false
    const { data=[] } =res
    if(data.length == 0) ToastWarning(`彩蛋ID：${form.color_gift_id}下暂无彩蛋礼物`)
    serachGiftRef.value.setList(data)
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const { id = 0 } = form
      const loading = Toastloading()
      let submitSet = false
      if (id) {
        await propApi.colorWhiteEdit(form).then(() => {
          submitSet = true
          ToastSuccess('编辑成功')
        }).catch(() => {
          loading.close()
        });
      }else {
        await propApi.colorWhiteAdd(form).then(() => {
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(() => {
          loading.close()
        });
      }
      if (submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any = {})=> {
    const { id, color_gift_id, user_id, pop_gift_id, delete_pop, nickname='' } = query
    const obj = {
      color_gift_id, user_id, pop_gift_id, delete_pop
    } as any
    if(id) {
      obj['id'] = id
    } else {
      nextTick(()=>{
        if(serachGiftRef.value) serachGiftRef.value.removeQuery()
      })
    }
    Object.assign(form,obj)
    action.dialogFormVisible = true
    action.title = id?`编辑彩蛋白名单：${nickname}`:'新增彩蛋白名单'
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" prop="color_gift_id" label="彩蛋礼物ID">
          <el-input @input="colorGiftInput" v-model="form.color_gift_id" placeholder="请输入彩蛋礼物ID" />
          <el-button :loading="action.serachButLoadng" class="xm-ml10" type="primary" @click="getGiftList">查询彩蛋礼物</el-button>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="user_id" label="用户UUID">
          <serachUserid v-model:user_id="form.user_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="pop_gift_id" label="爆出礼物ID">
          <serachGift ref="serachGiftRef" v-model:gift_id="form.pop_gift_id" :externalApi="true" @getList="getGiftList" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="delete_pop" label="是否从礼物池中扣除">
          <el-select v-model="form.delete_pop" placeholder="请选择日期类型">
            <el-option label="是" :value="1" />
            <el-option label="否" :value="0" />
          </el-select>
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
